//
//  CollectionViewController.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/25.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "CollectionViewController.h"
#import "UITableView+EmptyData.h"
#import "ShopCarTableViewCell.h"
@interface CollectionViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSMutableArray *dataSource;

@end

@implementation CollectionViewController
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource=[NSMutableArray array];
    }
    return _dataSource;
}
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView=[[UITableView alloc]initWithFrame:self.view.frame];
        _tableView.delegate=self;
        _tableView.dataSource=self;
        [self.tableView registerNib:[UINib nibWithNibName:@"ShopCarTableViewCell" bundle:nil] forCellReuseIdentifier:@"ShopCarTableViewCell"];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"我的收藏";
    [self.view addSubview:self.tableView];

    // Do any additional setup after loading the view.
}

#pragma mark----tableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return [tableView showMessage:@"没有数据" byDataSourceCount:self.dataSource.count];

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ShopCarTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ShopCarTableViewCell" forIndexPath:indexPath];
//    [cell setCellWithReadDetailModel:self.dataSource[indexPath.row]];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

#pragma mark---tableViewDelegate协议

//让tableview进入可编辑模式
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

//设置tableView的编辑模式
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    //进入删除模式
    return UITableViewCellEditingStyleDelete;
}

//当tableViewCell编辑的时候执行的事件
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{

    if (editingStyle==UITableViewCellEditingStyleDelete) {

        //删除本地数据
//        [[DataManager sharedManager]deleteData:self.dataSource[indexPath.row]];
        //删除数据源
        [self.dataSource removeObjectAtIndex:indexPath.row];
        //删除UI
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
